#include "ContentDisposition.h"

#include <boost/algorithm/string.hpp>

namespace rdhttp {

    void ContentDisposition::pares(std::string_view content) {
        data = content;

        if (data.empty())
            return;
        // Content-Disposition: form-data; filename=D:/Users/SGA/Pictures/clolse.png
        boost::erase_all(data, " ");

        auto value_end = data.find_first_of(';');
        m_value        = data.substr(0, value_end);
        boost::to_lower(m_value);

        auto filename_start = data.find("filename=");
        if (filename_start == std::string::npos) return;

        // form-data;filename=clolse.png
        filename_start    = data.find_first_of('=', filename_start) + 1;
        auto filename_end = data.find_first_of(';', filename_start);

        m_filename = data.substr(filename_start, filename_end - filename_start);
        boost::erase_all(m_filename, "\"");
    }

    std::string ContentDisposition::value() const {
        return m_value;
    }

    std::string ContentDisposition::filename() const {
        return m_filename;
    }

    ContentDisposition::ContentDisposition(std::string_view content) {
        pares(content);
    }

    ContentDisposition::ContentDisposition() {
    }

    ContentDisposition::~ContentDisposition() {
    }

}  // namespace rdhttp
